package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.common.exception.BusinessException;
import com.demo.pojo.dto.ClassListDTO;
import com.demo.pojo.entity.ClassInfo;
import com.demo.service.ClassService;
import com.demo.mapper.ClassMapper;
import io.micrometer.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @author lenovo
* @description 针对表【class】的数据库操作Service实现
* @createDate 2025-09-17 10:15:14
*/
@Service
@Slf4j
public class ClassServiceImpl extends ServiceImpl<ClassMapper, ClassInfo>
    implements ClassService{

    private final ClassMapper classMapper;

    public ClassServiceImpl(ClassMapper classMapper) {
        this.classMapper = classMapper;
    }

    @Override
    public ClassInfo selectById(Integer id) {
        //根据班级id查询班级信息
        ClassInfo clazz = classMapper.selectById(id);
        //如果没查到抛出异常
        if (clazz == null){
            throw new BusinessException("没有此班级信息");
        }
        log.info("查询班级信息成功:{}", clazz);
        return clazz;
    }

    @Override
    public void saveClass(ClassInfo clazz) {
        // 验证必填字段
        if (clazz.getClName() == null || clazz.getClName().trim().isEmpty()) {
            throw new BusinessException("班级名称不能为空");
        }
        if (clazz.getClGrade() == null) {
            throw new BusinessException("年级不能为空");
        }
        if (clazz.getClMajor() == null || clazz.getClMajor().trim().isEmpty()) {
            throw new BusinessException("专业不能为空");
        }
        
        //新增前判断班级名称有无重复
        if (classMapper.selectByName(clazz.getClName()) != null){
            throw new BusinessException("班级名称重复");
        }
        
        // 设置创建时间
        clazz.setClUpdateDate(new Date());
        
        //执行新增操作
        int result = classMapper.insert(clazz);
        if (result <= 0) {
            throw new BusinessException("新增班级失败");
        }
        log.info("新增班级信息成功:{}", clazz);
    }

    @Override
    public void updateClass(ClassInfo clazz) {
        //修改前判断是否存在该班级
        ClassInfo oldClass = classMapper.selectById(clazz.getClId());
        if (oldClass == null){
            throw new BusinessException("没有此班级信息");
        }
        //修改前判断班级名称有无变动
        //如果查询出的班级名称和修改前的班级名称一致，则不进行修改
        if (!oldClass.getClName().equals(clazz.getClName())){
           //班级名称不一致，说明班级名称被修改，查询有无重复
            if (classMapper.selectByName(clazz.getClName()) != null){
                throw new BusinessException("班级名称重复");
            }
        }
        //设置修改时间
        clazz.setClUpdateDate(new Date());
        //执行修改操作
        classMapper.updateById(clazz);
        log.info("修改班级信息成功:{}", clazz);
    }

    @Override
    public Map<String, Object> listPage(ClassListDTO dto) {
        //1. 创建分页对象
        Page<ClassInfo> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        // 2. 构建查询条件
        LambdaQueryWrapper<ClassInfo> lambdaQueryWrapper = new LambdaQueryWrapper<ClassInfo>()
                .like(StringUtils.isNotBlank(dto.getClName()),ClassInfo::getClName,dto.getClName())
                .eq(dto.getClGrade() != null,ClassInfo::getClGrade,dto.getClGrade())
                .like(StringUtils.isNotBlank(dto.getClMajor()),ClassInfo::getClMajor,dto.getClMajor())
                ;

        // 3. 执行分页查询
        Page<ClassInfo> pageResult = classMapper.selectPage(page, lambdaQueryWrapper);

        log.info("分页查询结果：{}", pageResult);
        // 4. 返回结果
        return Map.of(
                "records", pageResult.getRecords(),
                "total", pageResult.getTotal(),
                "current", pageResult.getCurrent()
        );
    }

    @Override
    public List<ClassListDTO> getClassList() {
        // 查询所有班级
        List<ClassInfo> classes = list();
        List<ClassListDTO> classListDTOs = new ArrayList<>();
        
        for (ClassInfo clazz : classes) {
            ClassListDTO dto = new ClassListDTO();
            dto.setClId(clazz.getClId());
            dto.setClName(clazz.getClName());
            dto.setClGrade(clazz.getClGrade());
            dto.setClMajor(clazz.getClMajor());
            dto.setClRemark(clazz.getClRemark());
            dto.setClUpdateDate(clazz.getClUpdateDate());
            classListDTOs.add(dto);
        }
        
        return classListDTOs;
    }

}

